package com.example.yiqing;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Function {

    public  String getYiQingJson() {
        String httpUrl = "http://api.tianapi.com/ncov/index?key=95d19d8afe708666a1899ea16406824d";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public JSONObject getChina(){
         String YiQingJson = getYiQingJson();
         System.out.println("源码:"+ YiQingJson);
         try{
             JSONObject jsonObject = new JSONObject(YiQingJson);
             return jsonObject.getJSONArray("newslist").getJSONObject(0).getJSONObject("desc");
         }catch (Exception e){
             e.printStackTrace();
         }
         return null;
    }

    public JSONObject getGoble(){
        String YiQingJson = getYiQingJson();
        System.out.println("源码:"+YiQingJson);
        try{
            JSONObject jsonObject = new JSONObject(YiQingJson);
            return jsonObject.getJSONArray("newslist").getJSONObject(0).getJSONObject("desc").getJSONObject("globalStatistics");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


}
